﻿'====================================================================
' MainForm
'--------------------------------------------------------------------
' The main form of the LogFileGenerator project.
' It generates a log file that simulates an imaginary use of the
' Windows application Skype (6.0.0.126) in XML format.
' The user should declare the number of events and the time that
' the logging took place.
'====================================================================

Imports System.IO

Public Class LogFileGenerator
    Dim mstrMenuItem As MenuItem
    Dim mrndGenerator As New Random

    Structure MenuItem
        Dim sName As String
        Dim sngProb As Single
        Dim lstSubMenuItems As List(Of MenuItem)

        Sub New(ByVal _sName)
            sName = _sName
            lstSubMenuItems = New List(Of MenuItem)
        End Sub
    End Structure

    ' Prints in console the structure of Skype's main menu
    Private Sub PrintList(ByVal strMenuItem As MenuItem, ByVal iLevel As Integer)
        If strMenuItem.lstSubMenuItems.Count = 0 Then
            Exit Sub
        Else
            For Each strSubMenuItem In strMenuItem.lstSubMenuItems
                Select Case iLevel
                    Case 1
                        Console.Write(">>> ")
                    Case 2
                        Console.Write(">>>>> ")
                End Select
                Console.WriteLine(strSubMenuItem.sName)
                PrintList(strSubMenuItem, iLevel + 1)
            Next strSubMenuItem
        End If
    End Sub

    ' Returns the appropriate strMenuItem as the next action of an event
    Private Function PickAction(ByVal lstMainMenu As List(Of MenuItem), ByVal strMenuItem As MenuItem) As MenuItem
        If strMenuItem.lstSubMenuItems.Count = 0 Then
            Return lstMainMenu(mrndGenerator.Next(0, lstMainMenu.Count))
        Else
            Return strMenuItem.lstSubMenuItems(mrndGenerator.Next(0, strMenuItem.lstSubMenuItems.Count))
        End If
    End Function

    Private Sub btnGenerateLogFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerateLogFile.Click
        ' Define the structure of Skype's main menu
        Dim arrMainMenuItems() As MenuItem = {New MenuItem("Skype"), New MenuItem("Contacts"), New MenuItem("Call"), New MenuItem("View"), New MenuItem("Tools"), New MenuItem("Help")}
        Dim arrSkypeMenuItems() As MenuItem = {New MenuItem("Online Status"), New MenuItem("Profile"), New MenuItem("Privacy..."), New MenuItem("Account..."), New MenuItem("Buy Skype Credit..."), New MenuItem("Change Password..."), New MenuItem("Sign Out"), New MenuItem("Close")}
        Dim arrOnlineStatusMenuItems() As MenuItem = {New MenuItem("Online"), New MenuItem("Away"), New MenuItem("Do Not Disturb"), New MenuItem("Invisible"), New MenuItem("Offline"), New MenuItem("Set up Call Forwarding...")}
        Dim arrProfileMenuItems() As MenuItem = {New MenuItem("Change Your Picture..."), New MenuItem("Change Sounds..."), New MenuItem("Edit Your Profile...")}
        Dim arrContactMenuItems() As MenuItem = {New MenuItem("Add Contact"), New MenuItem("Import Contacts... "), New MenuItem("Create New Group..."), New MenuItem("Contact Lists"), New MenuItem("Show Outlook Contacts"), New MenuItem("Sort Contacts by"), New MenuItem("Hide Contacts Who"), New MenuItem("Advanced"), New MenuItem("Remove from Contacts")}
        Dim arrContactListsMenuItems() As MenuItem = {New MenuItem("All"), New MenuItem("Skype"), New MenuItem("Online"), New MenuItem("Facebook"), New MenuItem("Create New List..."), New MenuItem("Delete List 'All'..."), New MenuItem("Rename List 'All'...")}
        Dim arrSortContactsByMenuItems() As MenuItem = {New MenuItem("Name"), New MenuItem("Online Status")}
        Dim arrHideContactsWhoMenuItems() As MenuItem = {New MenuItem("Are Offline"), New MenuItem("Are Offline with Call Forwarding"), New MenuItem("Haven't Shared Contact Details")}
        Dim arrAdvancedMenuItems() As MenuItem = {New MenuItem("Backup Contacts to File..."), New MenuItem("Restore Contacts from File..."), New MenuItem("Manage Blocked Contacts...")}
        Dim arrCallMenuItems() As MenuItem = {New MenuItem("Call"), New MenuItem("Video Call"), New MenuItem("Answer"), New MenuItem("Ignore"), New MenuItem("Mute Microphone"), New MenuItem("Hold"), New MenuItem("Transfer Call..."), New MenuItem("Hang up"), New MenuItem("Show Dial Pad"), New MenuItem("Call Phones"), New MenuItem("Audio Settings..."), New MenuItem("Video"), New MenuItem("Share Settings..."), New MenuItem("Stop Sharing Screen"), New MenuItem("Learn about Call Quality")}
        Dim arrVideoMenuItems() As MenuItem = {New MenuItem("Hide My Video Automatically"), New MenuItem("Video Snapshot"), New MenuItem("Video Settings")}
        Dim arrViewMenuItems() As MenuItem = {New MenuItem("Contacts"), New MenuItem("Recent"), New MenuItem("Voice Messages"), New MenuItem("Files Sent and Received"), New MenuItem("SMS Messages"), New MenuItem("Instant Messages"), New MenuItem("Skype Home"), New MenuItem("Profile"), New MenuItem("Call Phones"), New MenuItem("Snapshots Gallery"), New MenuItem("Default View"), New MenuItem("Full Screen"), New MenuItem("Show Hidden Conversations")}
        Dim arrToolsMenuItems() As MenuItem = {New MenuItem("Apps"), New MenuItem("Change Language"), New MenuItem("Accessibility"), New MenuItem("Skype WiFi..."), New MenuItem("Options...")}
        Dim arrAppsMenuItems() As MenuItem = {New MenuItem("Get Apps...")}
        Dim arrChangeLanguageMenuItems() As MenuItem = {New MenuItem("English (English)"), New MenuItem("Ελληνικά (Greek)"), New MenuItem("Espanol (Spanish)"), New MenuItem("Francais (French)"), New MenuItem("Italiano (Italian)")}
        Dim arrAccessibilityMenuItems() As MenuItem = {New MenuItem("Extended Keyboard Navigation"), New MenuItem("Screen Reader Mode")}
        Dim arrHelpMenuItems() As MenuItem = {New MenuItem("Get Help: Answers and Support"), New MenuItem("Heartbeat (Skype Status)"), New MenuItem("Call Quality Guide"), New MenuItem("Check for Updates"), New MenuItem("Give feedback"), New MenuItem("Privacy Policy"), New MenuItem("About Skype")}

        ' Initialize a list that we will use it to implement our actions
        Dim lstMainMenu As New List(Of MenuItem)
        lstMainMenu.AddRange(arrMainMenuItems)
        lstMainMenu(0).lstSubMenuItems.AddRange(arrSkypeMenuItems)
        lstMainMenu(0).lstSubMenuItems(0).lstSubMenuItems.AddRange(arrOnlineStatusMenuItems)
        lstMainMenu(0).lstSubMenuItems(1).lstSubMenuItems.AddRange(arrProfileMenuItems)
        lstMainMenu(1).lstSubMenuItems.AddRange(arrContactMenuItems)
        lstMainMenu(1).lstSubMenuItems(3).lstSubMenuItems.AddRange(arrContactListsMenuItems)
        lstMainMenu(1).lstSubMenuItems(5).lstSubMenuItems.AddRange(arrSortContactsByMenuItems)
        lstMainMenu(1).lstSubMenuItems(6).lstSubMenuItems.AddRange(arrHideContactsWhoMenuItems)
        lstMainMenu(1).lstSubMenuItems(7).lstSubMenuItems.AddRange(arrAdvancedMenuItems)
        lstMainMenu(2).lstSubMenuItems.AddRange(arrCallMenuItems)
        lstMainMenu(2).lstSubMenuItems(11).lstSubMenuItems.AddRange(arrVideoMenuItems)
        lstMainMenu(3).lstSubMenuItems.AddRange(arrViewMenuItems)
        lstMainMenu(4).lstSubMenuItems.AddRange(arrToolsMenuItems)
        lstMainMenu(4).lstSubMenuItems(0).lstSubMenuItems.AddRange(arrAppsMenuItems)
        lstMainMenu(4).lstSubMenuItems(1).lstSubMenuItems.AddRange(arrChangeLanguageMenuItems)
        lstMainMenu(4).lstSubMenuItems(2).lstSubMenuItems.AddRange(arrAccessibilityMenuItems)
        lstMainMenu(5).lstSubMenuItems.AddRange(arrHelpMenuItems)

        ' Take the input from the controls and define needed variables
        Dim iEvents As Integer = txtNumberOfEvents.Text
        Dim dteFrom As Date = dtpFrom.Value
        Dim dteTo As Date = dtpTo.Value
        Dim dteDiff = dteTo - dteFrom
        Dim sngStep As TimeSpan
        Dim sLogFile As String

        ' Basic checks
        If dteFrom > dteTo Then
            MsgBox("The From date is later than the To Date!", MsgBoxStyle.Exclamation)
            Return
        End If
        ' Minimum: one event in one second
        If dteDiff.TotalSeconds > iEvents Then
            sngStep = New TimeSpan(0, 0, dteDiff.TotalSeconds / iEvents)
        End If

        ' Create the XML log file
        mstrMenuItem = lstMainMenu(mrndGenerator.Next(0, lstMainMenu.Count))
        sLogFile = "<logfile>" & vbNewLine
        Dim bFirstTime As Boolean = True
        For iEvent = 0 To iEvents
            dteFrom = dteFrom.Add(sngStep)
            sLogFile &= "<event>" & vbNewLine
            sLogFile &= "<date>" & vbNewLine
            sLogFile &= dteFrom.ToString("dd/MM/yyyy") & vbNewLine
            sLogFile &= "</date>" & vbNewLine
            sLogFile &= "<time>" & vbNewLine
            sLogFile &= dteFrom.ToString("HH:mm:ss") & vbNewLine
            sLogFile &= "</time>" & vbNewLine
            sLogFile &= "<action>" & vbNewLine
            If bFirstTime Then
                bFirstTime = False
                sLogFile &= mstrMenuItem.sName
            Else
                mstrMenuItem = PickAction(lstMainMenu, mstrMenuItem)
                sLogFile &= mstrMenuItem.sName
            End If
            sLogFile &= "</action>" & vbNewLine
            sLogFile &= "</event>" & vbNewLine
        Next iEvent
        sLogFile &= "</logfile>" & vbNewLine

        ' Save File Dialog
        SaveLogFileDialog.Filter = "XML Files|*.xml"
        SaveLogFileDialog.Title = "Save Generated Log File"
        If SaveLogFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            My.Computer.FileSystem.WriteAllText(SaveLogFileDialog.FileName, sLogFile, False)
        End If
    End Sub

    Private Sub txtNumberOfEvents_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNumberOfEvents.TextChanged
        If txtNumberOfEvents.Text.Length <> 0 Then
            If IsNumeric(txtNumberOfEvents.Text) Then
                btnGenerateLogFile.Enabled = True
            Else
                MsgBox("The number of events should be a number.", MsgBoxStyle.Exclamation)
                btnGenerateLogFile.Enabled = False
            End If
        Else
            btnGenerateLogFile.Enabled = False
        End If
    End Sub

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dtpFrom.Value = Now()
        dtpTo.Value = (dtpFrom.Value).AddDays(1)
    End Sub
End Class
